package guanqu.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.beans.IrrBRMeteorological;
import guanqu.beans.IrrBRRain;
import manager.PersistentManager;

/**
 * Servlet implementation class GetRainWeater
 */
//@WebServlet("/GetRainWeater")
public class GetRainWeater extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetRainWeater() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		
		Download download = new Download();
		
		String stadiaCode = request.getParameter("data");
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			Query q1 = session.createQuery("from IrrBRMeteorological lg where lg.stadiaCode = ? order by lg.meaTime desc");
			q1.setParameter(0, stadiaCode);
			download.setBrMeteorologicals(q1.list());
			
			Query q2 = session.createQuery("from IrrBRRain r where r.stadiaCode = ? order by r.meaTime desc");
			q2.setParameter(0, stadiaCode);
			download.setBrRains(q2.list());
			
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		}finally {
//			session.close();
		}

		System.out.println(JSON.toJSONString(download));
		response.setCharacterEncoding("UTF-8");
		response.getWriter().append(JSON.toJSONString(download));
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	// 下行数据
	public static class Download
	{
		
		int status;
		List<IrrBRRain> brRains;
		List<IrrBRMeteorological> brMeteorologicals;
		
		
		
		public int getStatus() {
			return status;
		}
		public void setStatus(int status) {
			this.status = status;
		}
		public List<IrrBRRain> getBrRains() {
			return brRains;
		}
		public void setBrRains(List<IrrBRRain> brRains) {
			this.brRains = brRains;
		}
		public List<IrrBRMeteorological> getBrMeteorologicals() {
			return brMeteorologicals;
		}
		public void setBrMeteorologicals(List<IrrBRMeteorological> brMeteorologicals) {
			this.brMeteorologicals = brMeteorologicals;
		}
		
		
	}

}
